package com.karst.essentials.chapter06.galaxyExample
{
	import org.papervision3d.core.proto.GeometryObject3D;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.objects.primitives.Sphere;
	
	internal class Galaxy extends DisplayObject3D
	{
		public function Galaxy()
		{
			super();
			createView() ;
		}

		private var shadeMaterial : BitmapFileMaterial = null ;
		private var earthSphere : Sphere = null ;
		private function createView() : void
		{
			if( shadeMaterial == null )
				shadeMaterial = new BitmapFileMaterial( "earth.jpg" ) ;
			if( earthSphere == null )
				earthSphere = new Sphere( shadeMaterial, 200, 24, 26 ) ;
			addChild( earthSphere ) ;
			
			for( var i : int = 0; i < 150; i++ )
			{
				var plane : Plane = new Plane( 
					new ColorMaterial( Math.random() * 0xFFFFFF ), 50, 50, 1, 1 ) ;
				plane.material.doubleSided = true ;
				addChild( plane ) ;
				plane.x = Math.random() * 20000 - 10000 ;
				plane.y = Math.random() * 1500 - 750 ;
				plane.z = Math.random() * 20000 - 10000 ;
				plane.localRotationY = Math.random() * 180 - 90 ;
			}
		}
		
		public function update() : void
		{
			if( earthSphere )
			{
				earthSphere.localRotationY += .2 ;
			}
		}
	}
}